home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / examples / smoothing / ex5.d < prev    next >
Encoding:
Text File  |  1991-03-10  |  3.8 KB  |  129 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. /* This program performs a nine-point smoothing, where each point is updated
  3.  * to be the average of his eight neighbors.  The matrix is M x N, where
  4.  * M is a multiple of P1, and N is a multiple of P2.  The use of asynchronous
  5.  * variables is demonstrated here. */
  6.  
  7. #include "dino.h"
  8.  
  9. #define max(x,y) (x > y ? x : y)
  10. #define min(x,y) (x < y ? x : y)
  11.  
  12. #define M 12
  13. #define N 8
  14. #define P1 4
  15. #define P2 4
  16.  
  17. environment node[P1:id1][P2:id2] {
  18.  
  19.   composite smooth (a, in iter)
  20.   double /*asynch*/ distributed a[M][N] map NinePt;
  21.  
  22.                                 /* ==> The NinePt mapping is just like FivePt,
  23.                                        except that the four corner overlap
  24.                                        elements are included in the overlap. */
  25.  
  26.                                 /* ==> Note the use of the "asynch" keyword,
  27.                                        making communications using the matrix a
  28.                                        non-blocking.  This program will run
  29.                                        either synchronous or asynchronous. */
  30.  
  31.   int iter;
  32.  
  33.   {
  34.     int i, j, k;                           /* Looping variables */
  35.  
  36.     int home_n, home_s, home_w, home_e;    /* Boundaries of the home data */
  37.     int copy_n, copy_s, copy_w, copy_e;    /* Boundaries of the copy data */
  38.  
  39.     /* Compute home_n, home_s, home_w, and home_e */
  40.     home_n = max (M/P1 * id1, 1);
  41.     home_s = min ((id1 + 1) * M/P1 - 1, M-2);
  42.     home_w = max (N/P2 * id2, 1);
  43.     home_e = min ((id2 + 1) * N/P2 - 1, N-2);
  44.  
  45.     /* Compute copy_n, copy_s, copy_w, and copy_e */
  46.     copy_n = max (home_n - 1, 1);
  47.     copy_s = min (home_s + 1, M-2);
  48.     copy_w = max (home_w - 1, 1);
  49.     copy_e = min (home_e + 1, N-2);
  50.  
  51.     /* Repeat the smoothing process iter times */
  52.     for (i = 0; i < iter; i++) {
  53.  
  54.       /* Send out your data and receive it back again, if not the first
  55.        * iteration */
  56.       if (i != 0) {
  57.         a[<home_n,home_s>][<home_w,home_e>]# =
  58.           a[<home_n,home_s>][<home_w,home_e>];
  59.         a[<copy_n,copy_s>][<copy_w,copy_e>]#;
  60.       }
  61.  
  62.       /* Perform the computation, but only on non-edge elements */
  63.       for (j = home_n; j <= home_s; j++)
  64.         for (k = home_w; k <= home_e; k++) {
  65.           a[j][k] = a[j-1][k-1] +
  66.                     a[j-1][k] +
  67.                     a[j-1][k+1] +
  68.                     a[j][k-1];
  69.  
  70.                                 /* ==> The computation has been split up into
  71.                                        three separate pieces because some
  72.                                        compiler's can't handle the large
  73.                                        expression produced by DINO. */
  74.  
  75.           a[j][k] += a[j][k+1] +
  76.                      a[j+1][k-1] +
  77.                      a[j+1][k] +
  78.                      a[j+1][k+1];
  79.  
  80.           a[j][k] /= 8;
  81.  
  82.         }
  83.     }
  84.   }
  85. }
  86.  
  87. environment host {
  88.  
  89.   void main ()
  90.  
  91.   {
  92.     double a[M][N];                     /* Input data */
  93.     int iter;                           /* Holds the iteration count */
  94.  
  95.     int i, j;                           /* Looping variables */
  96.  
  97.     /* Set up the initial data for a[][] */
  98.     for (i = 0; i < M; i++)
  99.       for (j = 0; j < N; j++)
  100.         a[i][j] = (i + 1)*(j + 1);
  101.     for (i = 1; i < M - 1; i++)
  102.       for (j = 1; j < N - 1; j++)
  103.         a[i][j] = 0;
  104.  
  105.     /* Set up the variable which will contain the number of iterations */
  106.     iter = 300;
  107.  
  108.     /* Print out the initial data */
  109.     printf ("Initial data for a:\n");
  110.     for (i = 0; i < M; i++) {
  111.       for (j = 0; j < N; j++)
  112.         printf ("%7.2f", a[i][j]);
  113.       printf ("\n");
  114.     }
  115.  
  116.     /* Perform the computation */
  117.     smooth (a[][], iter)#;
  118.  
  119.     /* Printout the results */
  120.     printf ("Result data for a:\n");
  121.     for (i = 0; i < M; i++) {
  122.       for (j = 0; j < N; j++)
  123.         printf ("%7.2f", a[i][j]);
  124.       printf ("\n");
  125.     }
  126.   }
  127. }
  128.  
  129.